package com.shisfish.library.Controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.shisfish.library.Impl.ArrearageBookImpl;
import com.shisfish.library.Impl.ArrearageUserImpl;
import com.shisfish.library.Impl.UserImpl;
import com.shisfish.library.Model.ArrearageBook;
import com.shisfish.library.Model.ArrearageUser;
import com.shisfish.library.Model.User;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("arrearageUser")
public class ArrearageUserController extends GlobalVariable {

	private ArrearageBookImpl arrearageBookImpl = new ArrearageBookImpl();
	private ArrearageUserImpl arrearageUserImpl = new ArrearageUserImpl();
	private UserImpl userImpl = new UserImpl();
	
	@GET
	@Path("test")
	@Produces("application/json; charset=utf-8")
	public void test() {
		
		// 找到书本欠费记录按employee排（sql解决）
		List<Map<String, Object>> arrearageBooks = arrearageBookImpl.selectArrearage();
		
		for (int i = 0, len = arrearageBooks.size(); i < len; i++) {
			
			Map<String, Object> arrearageBook = arrearageBooks.get(i);
			String employee = (String) arrearageBook.get("employee");
			BigDecimal arrearage = (BigDecimal) arrearageBook.get("arrearage_sum");
			// 查找相应的employee 看是否有记录
			List<ArrearageUser> arrearageUsers = arrearageUserImpl.selectByKey(employee);
			if (arrearageUsers.size() > 0) {
				
				ArrearageUser arrearageUser = arrearageUsers.get(0);
				arrearageUser.setArrearage(arrearage);
				
				int flag = arrearageUserImpl.updateOne(arrearageUser);
				if (flag == 1) {
					System.out.println("update ok");
				}
				
				
			} else {
				ArrearageUser arrearageUser = new ArrearageUser(employee, arrearage);
				
				int flag = arrearageUserImpl.insertOne(arrearageUser);
				if (flag == 1) {
					System.out.println("insert ok");
				}
			}
			
		}
		
	}
	
	/**
	 * @author shisfish
	 * @date 2016年3月11日 下午10:43:27
	 * @Description 清空个人欠费记录
	 * @param idadmin
	 * @param loanid
	 * @return
	 */
	@GET
	@Path("clearArrearage/{idadmin}/{loanid}")
	@Produces("application/json; charset=utf-8")
	public String clearArrearage(@PathParam("idadmin") String idadmin, @PathParam("loanid") String loanid) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<User> users = userImpl.selectByLoanid(loanid);
		if (users.size() > 0) {
			
			String employee = users.get(0).getEmployee();
			
			// 找到arrearage_book 中的数据清空arrearage
			List<ArrearageBook> arrearageBooks = arrearageBookImpl.selectByEmployee(employee);
			if (arrearageBooks.size() > 0) {
				for (int i = 0, len = arrearageBooks.size(); i < len; i++) {
					ArrearageBook arrearageBook = arrearageBooks.get(i);
					arrearageBook.setArrearage(new BigDecimal(0));
					arrearageBookImpl.updateOne(arrearageBook);
				}
			} else if (arrearageBooks.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
			//找到arrearage_user 中的数据清空arrearage
			List<ArrearageUser> arrearageUsers = arrearageUserImpl.selectByKey(employee);
			if (arrearageUsers.size() > 0) {
				ArrearageUser arrearageUser = arrearageUsers.get(0);
				arrearageUser.setArrearage(new BigDecimal(0));
				int flag = arrearageUserImpl.updateOne(arrearageUser);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else if (arrearageUsers.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else if (users.size() == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		} else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @author shisfish
	 * @date 2016年3月11日 下午10:44:24
	 * @Description 通过employee查询欠费记录
	 * @param employee
	 * @return
	 */
	@GET
	@Path("selectByEmployee/{employee}")
	@Produces("application/json; charset=utf-8")
	public String selectByEmployee(@PathParam("employee") String employee) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		Map<String, Object> arrearageUser = arrearageUserImpl.selectEM(employee);
		
		if (arrearageUser != null) {
			globalMap.put("obj", arrearageUser);
		} else {
			ArrearageUser arrearageUser1 = new ArrearageUser(employee, new BigDecimal(0));
			globalMap.put("obj", arrearageUser1);
		}
		globalMap.put(STATUS_CODE, SUCCESS);
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByLoanid/{loanid}")
	@Produces("application/json; charset=utf-8")
	public String selectByLoanid(@PathParam("loanid") String loanid) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		List<User> users = new UserImpl().selectByLoanid(loanid);
		if (users.size() > 0) {
			String employee = users.get(0).getEmployee();
			Map<String, Object> arrearageUser = arrearageUserImpl.selectEM(employee);
			
			if (arrearageUser != null) {
				globalMap.put("obj", arrearageUser);
			} else {
				ArrearageUser arrearageUser1 = new ArrearageUser(employee, new BigDecimal(0));
				globalMap.put("obj", arrearageUser1);
			}
			globalMap.put(STATUS_CODE, SUCCESS);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @author shisfish
	 * @date 2016年3月11日 下午10:45:24
	 * @Description 查询全部欠费记录（分页）
	 * @param employee
	 * @return
	 */
	@GET
	@Path("selectAll/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectAll(@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		int count = arrearageUserImpl.countAll();
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			List<Map<String, Object>> arrearageUsers = arrearageUserImpl.selectAllPage(page.getBeginIndex(), pageSize);
			if (arrearageUsers.size() > 0) {
				globalMap.put(STATUS_CODE, SUCCESS);
				globalMap.put("list", arrearageUsers);
				globalMap.put("page", page);
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		globalMap.put(STATUS_CODE, SUCCESS);
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByEmployeeDetail/{employee}")
	@Produces("application/json; charset=utf-8")
	public String selectByEmployeeDetail(@PathParam("employee") String employee) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<Map<String, Object>> arrearageBooks = new ArrearageBookImpl().selectEM(employee);
		
		if (arrearageBooks.size() > 0) {
			globalMap.put("list", arrearageBooks);
		}
		globalMap.put(STATUS_CODE, SUCCESS);
		return toJsonUtils.map2Json(globalMap);
	}
	
}
